# these are generally in order from most specific to least,
# since first rule that fires wins.

include fileaddr

# declarations of ports without rules
plumb to seemail
plumb to showmail

# should deal with all videos etc
type is text
data matches '(gopher|https?)://.*\.(mkv|mp4|avi|webm|ogg|ogv|gifv|mp3|mp4|opus|flac|ape)'
plumb start mpv --no-terminal $0

# youtube and similar services
type is text
data matches 'https?://(www\.)?(gfycat\.|youtube\.|youtu\.be|vimeo\.com|twitter\.com/i/videos|bandcamp\.com|invidio\.us|bitchute\.com|lbry\.tv|redgifs\.com)[a-zA-Z0-9_@\-:]*([.:][a-zA-Z0-9_@\-]+)*/?[a-zA-Z0-9_?,%#~&/\-+=]+([:.][a-zA-Z0-9_?,%#~&/\-+=]+)*'
plumb to web
plumb start mpv --no-terminal $0

type is text
data matches 'https://doi.org/.*'
plumb to web
plumb start surf_tor sci-hub.tw/$0

type is text
data matches '(gopher|https?)://[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-]+)*/?[a-zA-Z0-9_?,%#~&/\-+=]+([:.][a-zA-Z0-9_?,%#~&/\-+=]+)*\.(txt|log|sh|TXT|LOG|SH)$'
plumb start sh -c 'curl -H 'User-Agent:' -sL '$0' | tr -d $"\r" | plumb -i -d edit -a ''action=showdata filename=/url/'$0'''
 
type is text
data matches 'https?://(www\.)?(imgur\.com)[a-zA-Z0-9_@\-]*([.:][a-zA-Z0-9_@\-]+)*/?[a-zA-Z0-9_?,%#~&/\-+=]+([:.][a-zA-Z0-9_?,%#~&/\-+=]+)*'
plumb to web
plumb start sh -c 'links="$(albumrip '$0')"; for link in $links; do mkdir -p /tmp/imgur/; tmp=$(mktemp /tmp/imgur/XXXX); curl -x socks5h://localhost:9050 -s $link > $tmp && echo $tmp; done| sxiv -i && rm /tmp/imgur/*'

type is text
data matches '(gopher|https?)://[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-]+)*/?[a-zA-Z0-9_?,%#~&/\-+=]+([:.][a-zA-Z0-9_?,%#~&/\-+=]+)*\.(jpe?g|JPE?G|gif|GIF|tiff?|TIFF?|ppm|bit|png|PNG)(\?[a-zA-Z0-9_@\-&]*)?'
plumb start sh -c 'tmp=$(mktemp); curl -H 'User-Agent:' -sL '$0' > $tmp ;sxiv $tmp; rm $tmp' 

# magnet links
type is text
data matches 'magnet:\?xt=.*'
plumb to web
plumb start sh -c 'transmission-remote -a $(echo "'$0'" | sed -E "s/dn=.*/dn=ab/g")'

# urls go to web browser
type is text
data matches '(https?|ftp|file|news|nntp|telnet|wais|prospero)://[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-()]+)*/?[a-zA-Z0-9_?,%#;~&/\-+=()]+([:.][a-zA-Z0-9_?,%#;~&/\-+=()]+)*'
plumb to web
plumb start palemoon $0

type is text
data matches 'gopher://[a-zA-Z0-9_@\-]+([.:][a-zA-Z0-9_@\-()]+)*/?[a-zA-Z0-9_?,%#;~&/\-+=()]+([:.][a-zA-Z0-9_?,%#;~&/\-+=()]+)*'
plumb to web
plumb start palemoon $0

type is text
data matches '.*\.html?'
arg isfile $0
plumb to web
plumb start palemoon $file

# doc and rtf files go to wdoc2txt
type is text
data matches '[a-zA-Z¡-￿0-9_\-./]+'
data matches '([a-zA-Z¡-￿0-9_\-./]+)\.(doc|rtf)'
arg isfile	$0
plumb to msword
plumb start loffice $file

# start rule for microsoft word documents without .doc suffix
type is text
dst is msword
plumb to msword
plumb start loffice $file

# image files go to sxiv
type is text
data matches '[a-zA-Z¡-￿0-9_\-./]+'
data matches '([a-zA-Z¡-￿0-9_\-./]+)\.(jpe?g|JPE?G|gif|GIF|tiff?|TIFF?|ppm|bit|png|PNG)'
arg isfile	$0
plumb to image
plumb start sxiv $file

type is text
data matches '.*\.(mkv|avi|webm|ogg|ogv|gifv|mp3|mp4|opus|flac|ape)'
arg isfile	$0
plumb to pager
plumb start mpv --no-terminal $file

# image files go to sxiv
type is text
data matches 'file://(([a-zA-Z¡-￿0-9_\-./]+)\.(jpe?g|JPE?G|gif|GIF|tiff?|TIFF?|ppm|bit|png|PNG))'
arg isfile	$1
plumb to image
plumb start sxiv $file

# postscript/pdf/dvi/epub go to zathura but not over the a plumb port
# the port is here for reference but is unused
type is text
data matches '.*\.(EPUB|epub|ps|PS|eps|EPS|pdf|PDF|dvi|DVI|djvu|DJVU)'
arg isfile $0
plumb to pager
plumb start zathura $file

# existing files tagged by line number:columnumber or linenumber.columnumber, twice, go to editor
type is text
data matches '([.a-zA-Z¡-￿0-9_/\-]*[a-zA-Z¡-￿0-9_/\-])':$twocolonaddr,$twocolonaddr
arg isfile     $1
data set       $file
attr add       addr=$2-#1+#$3,$4-#1+#$5
plumb to edit
plumb client $editor

# existing files tagged by line number:columnumber or linenumber.columnumber, twice, go to editor
type is text
data matches '([.a-zA-Z¡-￿0-9_/\-]*[a-zA-Z¡-￿0-9_/\-])':$twocolonaddr
arg isfile     $1
data set       $file
attr add       addr=$2-#1+#$3
plumb to edit
plumb client $editor

# existing files, possibly tagged by line number, go to editor
type is text
data matches '([.a-zA-Z¡-￿0-9_/\-]*[a-zA-Z¡-￿0-9_/\-])('$addr')?'
arg isfile	$1
data set	$file
attr add	addr=$3
plumb to edit
plumb client $editor

# .h files are looked up in /usr/include and passed to edit
type is text
data matches '([a-zA-Z¡-￿0-9/_\-]+\.h)('$addr')?'
arg isfile	/usr/include/$1
data set	$file
attr add	addr=$3
plumb to edit
plumb client $editor

# .h files are looked up in /usr/local/include and passed to edit
type is text
data matches '([a-zA-Z¡-￿0-9/_\-]+\.h)('$addr')?'
arg isfile	/usr/local/include/$1
data set	$file
attr add	addr=$3
plumb to edit
plumb client $editor

# .h files are looked up in $plan9/include and passed to edit
type is text
data matches '([a-zA-Z¡-￿0-9/_\-]+\.h)('$addr')?'
arg isfile	$plan9/include/$1
data set	$file
attr add	addr=$3
plumb to edit
plumb client $editor

# .m files are looked up in /usr/inferno/module and passed to edit
type is text
data matches '([a-zA-Z¡-￿0-9/_\-]+\.m)('$addr')?'
arg isfile	/usr/inferno/module/$1
data set	$file
attr add	addr=$3
plumb to edit
plumb client st $editor

# faces -> new mail window for message
type	is	text
data	matches	'[a-zA-Z¡-￿0-9_\-./]+'
data	matches	'/mail/fs/[a-zA-Z¡-￿0-9/]+/[0-9]+'
plumb	to	showmail
plumb	start	window -r 4 120 750 600 upas/nedmail -s $0

# email addresses get a new mail window
type is text
data matches '(mailto:)?([a-zA-Z0-9_+.\-]+@[a-zA-Z0-9_+.\-]*)'
plumb to sendmail
plumb start st mutt $0
# plumb start window rc -c '''echo % mail '''$0'; mail '$0

# man index entries are synthesized
type is text
data matches '9([a-zA-Z¡-￿0-9_\-./]+)\(([1-8])\)'
plumb start 9 rc -c 'man '$2' '$1' | nobs | plumb -i -d edit -a ''action=showdata filename=/9man/'$1'('$2')'''

type is text
data matches '([a-zA-Z¡-￿0-9_\-./]+)\(([1-8])\)'
plumb start sh -c 'MANWIDTH=71 man '$2' '$1' | nobs | plumb -i -d edit -a ''action=showdata filename=/man/'$1'('$2')'''

# start rule for images without known suffixes
dst is image
arg isfile $data
plumb to image
plumb start 9 page $data

# start rule for postscript without known suffixes
dst is postscript
arg isfile $data
plumb start 9 page $data

# relative files as file: urls get made into absolute paths
type is text
data matches 'file:([.a-zA-Z¡-￿0-9_\-]([.a-zA-Z¡-￿0-9_/\-]*[a-zA-Z¡-￿0-9_/\-]))?'
arg isfile	$1
data set	file://$file
plumb start plumb $data

type is text
data matches 'file://(.*)'
arg isfile	$1
plumb start plumb $1

type is text
data matches '.*\..*'
plumb start glinks $0


